import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.components.CollidableComponent;
import com.almasb.fxgl.input.Input;
import com.almasb.fxgl.input.UserAction;
import javafx.geometry.Point2D;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;

import static com.almasb.fxgl.dsl.FXGL.getInput;

public class stickTest2 extends GameApplication {
    private boolean stop = false;
    private boolean pull = false;
    public Entity stick;
    public static double r = 0;


    //鼠标左键 获取第一次的坐标
    private double mouseStartX;
    private double mouseStartY;
    private boolean mouseKit = false;

    //如果 点击鼠标右键 则获取 棍子固定的第一次位置
    private boolean rightHit = false;
    private double stickStayX;
    private double stickStayY;

    //点击右键获取 角度和棍子坐标
    private double angle_temp;
    private boolean angleStay = false;


    private double mouseAnyX;
    private double mouseAnyY;

    private double mouseAngle;

    private double k;
    private double b;







    public Entity ball;
    @Override
    protected void initSettings(GameSettings gameSettings) {
        gameSettings.setWidth(800);
        gameSettings.setHeight(800);
        gameSettings.setTitle("StickTest");
//        gameSettings.setFullScreenAllowed(true);
//        gameSettings.setFullScreenFromStart(true);

    }
    @Override
    protected void initGame() {
        stick = FXGL.entityBuilder()
                .at(400 + 230,400 )

                .viewWithBBox("stick.png")
                .with(new CollidableComponent(true))
                .buildAndAttach();

        /**球大小半径15*/
        ball = FXGL.entityBuilder()
                .at(400 - 15,400 - 15)
                .viewWithBBox("ball.png")
                .buildAndAttach();

    }


    @Override
    protected void onUpdate(double tpf) {
        //获取鼠标坐标  并一直改变
        Input in = FXGL.getInput();
        Point2D cP = in.getMousePositionWorld();
        //System.out.println(cP.getX());


        double r = getAngle(ball.getX() + 15,ball.getY() + 15,cP.getX(),cP.getY());
        /*设置 旋转原点*/
        stick.setRotationOrigin(new Point2D(0,0));
        /*转化度数*/
        //System.out.println(r);
        /*球杆坐标*/
        mouseAnyX = cP.getX();
        mouseAnyY = cP.getY();
//        System.out.println(stickX);


        //鼠标点击左边，获取坐标结束
        if(mouseKit == false){
            mouseStartX = mouseAnyX;
            mouseStartY = mouseAnyY;
        }






        if(stop == false){
            getPosition(ball,r);
        }else if(stop == true){
            if(pull == true){

                pullStick();
            }
        }










    }


    /**返回读书*/
    public double getAngle(double x, double y, double x2, double y2){
        double tanx = (y - y2) / (x - x2);
        double sita = Math.atan(tanx) * (180 / Math.PI);
        double angle = 0;

        //第一象限
        if (x2 > x && y2 > y){
            angle = sita;
        }
        //第二象限
        else if (x2 <= x && y2 > y){
            angle = 90 - Math.abs(sita) + 90;
        }
        // 第三象限
        else if (x2 < x && y2 <= y){
            angle = Math.abs(sita) + 180;
        }
        //第四象限
        else if (x2 >= x && y2 < y){
            angle = 90 - Math.abs(sita) + 270;
        }

        return angle;
    }

    /**获取鼠标点击*/
    @Override
    protected void initInput() {

        getInput().addAction(new UserAction("move_ball") {
//            @Override
//            protected void onActionBegin() {
//                stop = true;
//
//            }
            @Override
            protected void onActionBegin() {
                //当第一次点击鼠标时，mouseleft=true
                mouseKit = true;

            }

            @Override
            protected void onAction() {
                // action continues (key is held), increase swing power
                pull = true;
            }

            @Override
            protected void onActionEnd() {
                // action finished (key is released), play hitting animation based on swing power
                pull = false;
            }

        }, MouseButton.PRIMARY);

        getInput().addAction(new UserAction("move_ball2") {
            @Override
            protected void onActionBegin() {
                stop = !stop;
                //当第一次点击鼠标右键或许棍子固定的第一次位置 将 rightHit赋为true停止获取
                rightHit = !rightHit;
                angleStay = !angleStay;
            }
//            @Override
//            protected void onAction() {
//                // action continues (key is held), increase swing power
//                pull = true;
//            }
        }, MouseButton.SECONDARY);

    }




    public void getPosition(Entity ball , double angle){
        double R = Math.pow(15 * 15 + 4 * 4, 0.5);
        double x,y;
        double a = 0;
        if(angle <= 90){
            a = angle - Math.toDegrees(Math.atan(4.0 / 15));
            x = (ball.getX() + 15  + R * Math.cos(Math.toRadians(a)) * 2.5);
            y = (ball.getY() + 15  + R * Math.sin(Math.toRadians(a)) * 2.5);

        }else if(angle <= 180){
            a = angle - Math.toDegrees(Math.atan(4.0 / 15)) - 90;
            x = (ball.getX() + 15  - R * Math.sin(Math.toRadians(a)) * 2.5);
            y = (ball.getY() + 15  + R * Math.cos(Math.toRadians(a)) * 2.5);
            a = 90 + a;
        }else if(angle <= 270){
            a = angle - 180 - Math.toDegrees(Math.atan(4.0 / 15));
            x = (ball.getX() + 15  - R * Math.cos(Math.toRadians(a)) * 2.5);
            y = (ball.getY() + 15  - R * Math.sin(Math.toRadians(a)) * 2.5);
            a = 180 + a;
        }else {
            a = 360 - angle + Math.toDegrees(Math.atan(4.0 / 15));
            x = (ball.getX() + 15  + R * Math.cos(Math.toRadians(a)) * 2.5);
            y = (ball.getY() + 15  - R * Math.sin(Math.toRadians(a)) * 2.5);
            a = 360 - a;
        }


        if(rightHit == false){
            stickStayX = x;
            stickStayY = y;

        }


        if(angleStay == false){
            angle_temp = angle;

        }


        stick.setRotation(angle);
        stick.setPosition(new Point2D(x,y));





    }

    public void pullStick(){


        //double len = Math.sqrt(Math.pow((mouseStartX - stickStayX), 2) + Math.pow((mouseStartY - stickStayY), 2));
        double x ,y;
        double a = getAngle(mouseStartX, mouseStartY, mouseAnyX, mouseAnyY);

//        System.out.println(a);
        double b = 360-a;
//        System.out.println(angle_temp);
        //求任意时刻 mouseStartX  mouseAnyX
        double temp = Math.sqrt((Math.pow((mouseStartX - mouseAnyX), 2) + Math.pow((mouseStartY - mouseAnyY), 2)));
        x = stickStayX + temp * Math.cos(Math.toRadians(b));
        y = stickStayY - temp * Math.cos(Math.toRadians(b)) * Math.tan(Math.toRadians(360 - angle_temp));
//        y = -(temp * Math.cos(Math.toRadians(b)) * Math.tan(Math.toRadians(360 - angle_temp)));



        stick.setRotation(angle_temp);
        stick.setPosition(new Point2D(x,y));
    }



    public static void main(String[] args) {
        launch(args);
    }
}
